import { ref } from "vue";
import { cartRequest } from "@/api/cartlist";

// **全局状态-保存购物车列表数据**
export let cartArr = ref([]);


// **获取购物车列表数据**
export async function loadCartList() {
  let token = localStorage.getItem("user-token");

  if (!token) {
    console.warn("未登录，购物车为空");
    cartArr.value = [];
    return;
  }

  try {
    let response = await cartRequest(token);
    console.log("购物车数据返回:", response.data);

    if (response.data && response.data.data) {
      cartArr.value = response.data.data; // ✅ 确保购物车数据更新
      
    } else {
      cartArr.value = []; // ✅ 购物车为空时防止 undefined
    }
  } catch (error) {
    console.error("购物车数据加载失败:", error);
    cartArr.value = [];
  }
}
